package com.ebelter.btcomlib.models.https;

import com.ebelter.btcomlib.models.https.interfaces.HttpResponse;
import com.ebelter.btcomlib.utils.GsonUtils;
import com.lzy.okgo.callback.StringCallback;
import okhttp3.Call;
import okhttp3.Response;
import static com.ebelter.btcomlib.models.https.NetUtils.R_F;
import static com.ebelter.btcomlib.models.https.NetUtils.R_S_R_F;
import static com.ebelter.btcomlib.models.https.NetUtils.R_S_R_S;


public class MyStringCallback<T> extends StringCallback {


    private Class<T> classOfT;
    private HttpResponse<T> httpResponse;



    public MyStringCallback(HttpResponse<T> httpResponse, Class<T> classOfT) {
        this.classOfT = classOfT;
        this.httpResponse = httpResponse;
    }

    @Override
    public void onSuccess(String s, Call call, Response response) {
        T t= GsonUtils.getInstance().fromJson(s,classOfT);
        if (t!=null){
            httpResponse.result(true,R_S_R_S,t,s);
        }else {
            httpResponse.result(false,R_S_R_F,null,s);
        }
    }

    @Override
    public void onError(Call call, Response response, Exception e) {
        super.onError(call, response, e);
        httpResponse.result(false,R_F,null,response!=null?response.code()+"":"");

    }
}
